<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//商品控制器
class Goods extends Home_Controller{

	public function __construct(){
		parent::__construct();
        $this->load->model('category_model');
		$this->load->model('goods_model');
        $this->load->model('attribute_model');
        //加载省市区模型
        $this->load->model('area_model');
        //加载分页
        $this->load->library('pagination');
	}

	public function index(){
	    $goods_id = $this->input->get("id");
		$data['goods'] =$goods_info= $this->goods_model->get_goods($goods_id);
        $goods_img = $goods_info['goods_img'];
        $data['goods_img'] = explode(',',$goods_img);
        $type_id = $goods_info['type_id'];

        //获取商品属性信息
        //$attr_input_type=1，也就是有多个值的
        $data['attributes']= $this->attribute_model->list_attrs($type_id,1);

        //商品参数信息

        $data['attrs'] = $this->goods_model->list_attrs($goods_id);

        //获取热卖商品
        $data['hots'] = $this->goods_model->hot_goods();

        //猜你喜欢获取点击量高的
        $data['hits'] = $this->goods_model->hit_goods();

        //获取省
        $data['provices']=$this->area_model->list_area(1);
        //获取市
        $data['citys']=$this->area_model->list_area(2);
        //获取县
        $data['countys']=$this->area_model->list_area(3);

        $data['cates'] = $this->category_model->front_cate();
        $this->load->view('public/header.html',$data);
        $this->load->view('goods.html');
	}

    public function lists(){
        $cat_id = $this->input->get("id");
        $data['cates'] = $this->category_model->front_cate();

        $data['goods_name']= $goods_name= $this->input->post("goods_name");

        $data['cat_id'] = $cat_id;
        //所有类别子类内容
        $data['list_child_cate']= $this->category_model->list_child_cate();

        $data['count_goods']=$this->goods_model->count_goods($cat_id,$goods_name);

        $this->load->view('public/header.html',$data);
        $this->load->view('lists.html');
    }


    //ajax分页数据
    public function list_ajax(){
        $cat_id = $this->input->get("id");
        $goods_name= $this->input->get("goods_name");
        //CI框架自带分页开始
        $config['base_url'] = base_url()."goods/list_ajax?id={$cat_id}";
        //每页显示的条数
        $config['per_page']=$limit=1;
        //放在你当前页码的前面和后面的“数字”链接的数量。比方说值为 2 就会在每一边放置 2 个数字链接，就像此页顶端的示例链接那样。
        $config['num_links'] = 4;
        $config['uri_segment'] = 3;
        $config['cur_tag_open'] = '<li class="am-active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] =  '</li>';
        $config['use_page_numbers'] = true;//默认分页URL中是显示每页记录数,启用use_page_numbers后显示的是当前页码，
        $config['first_link'] = '首页'; //你希望在分页的左边显示“第一页”链你不希接的名字。如果望显示，可以把它的值设为 FALSE 。
        $config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。如果你不希望显示，可以把它的值设为 FALSE 。
        $config['prev_link'] = '上一页';
        $config['next_link'] = '下一页';
        $config['page_query_string'] = true;
        //分页的总条数
        $config['total_rows']=$data['count_goods']=$this->goods_model->count_goods($cat_id,$goods_name);
        $this->pagination->initialize($config);//初始化分类页
        $data['page'] = @str_replace('href','ajaxhref',$this->pagination->create_links()); //输出的分页结构
        //分页结束
        if(!empty($_GET['per_page'])){
            $offset = $_GET['per_page']?($_GET['per_page']-1)* $limit:1;
        }else{
            $offset = $this->uri->segment(3)?$this->uri->segment(3):0;
        }
        $data['goods']=$this->goods_model->list_goods($limit,$offset,$cat_id,$goods_name);
        //精品推荐
        $data['bests'] = $this->goods_model->best_goods();
        $this->load->view('list_ajax.html',$data);
    }


}